package org.example.ddm;

import com.android.ddmlib.AndroidDebugBridge;
import com.android.ddmlib.IDevice;
import com.android.ddmlib.SyncService;
import com.android.ddmlib.SyncService.ISyncProgressMonitor;

public class DdmlibExample {
    public static void main(String[] args) {
        AndroidDebugBridge.init(false);
        AndroidDebugBridge bridge = AndroidDebugBridge.createBridge("adb", false);

        // 等待设备连接
        waitForDevice(bridge);

        IDevice[] devices = bridge.getDevices();
        if (devices.length == 0) {
            System.out.println("No devices connected.");
            return;
        }

        IDevice device = devices[0];
        try {
            SyncService syncService = device.getSyncService();
            String localFilePath = "C:/Users/YourName/Desktop/example.txt";
            String remoteFilePath = "/sdcard/Download/example.txt";

            // 推送文件
            syncService.pushFile(localFilePath, remoteFilePath, new ISyncProgressMonitor() {
                @Override
                public void start(int totalWork) {
                }

                @Override
                public void stop() {
                }

                @Override
                public boolean isCanceled() {
                    return false;
                }

                @Override
                public void startSubTask(String name) {
                }

                @Override
                public void advance(int work) {
                }
            });

            System.out.println("File pushed successfully.");

            syncService.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            AndroidDebugBridge.terminate();
        }
    }

    private static void waitForDevice(AndroidDebugBridge bridge) {
        int attempts = 0;
        while (!bridge.hasInitialDeviceList() && attempts < 10) {
            try {
                Thread.sleep(500);
                attempts++;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
